import Vue from "vue";
import Router from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import store from "vuex";
import initTheme from "@/components/common/ThemeSetting/initTheme";

import { getParameterByName } from "@/utils/utils";


Vue.use(Router);

const routes = [
  {
    path: "/",
    name: "home",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/pages/home/home.vue"),
  },
  // {
  //   path: '/smartEnergy',
  //   name: 'smartEnergy',
  //   component: () => import('@/pages/smartEnergy/smartEnergy.vue'),
  //   children: [
  //     {
  //       path: '',
  //       redirect: 'energyList',
  //     },
  //     {
  //       path: 'energyList',
  //       name: 'energyList',
  //       component: () => import('@/pages/smartEnergy/energyList.vue'),
  //     },
  //     {
  //       path: 'energyDetail/:id',
  //       name: 'energyDetail',
  //       component: () => import('@/pages/smartEnergy/energyDetail.vue'),
  //       props: true
  //     }
  //   ]
  // },
  
  {
    path: "/login",
    name: "login",
    component: () => import("@/pages/loginPage/login.vue"),
  },
  {
    path: "/user",
    name: "user",
    component: () => import("@/pages/user/user.vue"),
  },
];
const router = new Router({
  // mode: "history",
  routes,
});

const blackList = "";

router.beforeEach(async (to, from, next) => {
  NProgress.start();

  // 加载皮肤
  const styleId = getParameterByName("styleId" ,to.fullPath);
  const color = getParameterByName("color" ,to.fullPath.replace("#", ""));
  initTheme({styleId, color: color ? `#${color}` : null});
  const token = "";
  if (token) {
    // 用户信息不存在
    if (!store.getters.userInfo) {
      // await store.dispatch('GetUser'); // 获取用户信息
      // router.addRoutes(store.getters.addRoutes);
    } else {
      next();
    }
  } else if (blackList.includes(to.path)) {
    // window.location.href = store.getters.api.IPORTAL_LOCAL_API;
    NProgress.done();
  } else {
    // 不是黑名单，直接进入
    next();
  }
});
router.afterEach(() => {
  NProgress.done();
  router.afterEach((to, from, next) => {
    
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
  });
});
export default router;
